<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8" ref="refbut">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus"  @click="handleAdd" v-hasPermi="['archive:yqPgg:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit"  @click="handleUpdate" v-hasPermi="['archive:yqPgg:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete"  @click="handleDelete" v-hasPermi="['archive:yqPgg:remove']">删除</el-button>
      </el-col>
    </el-row>
    <el-table ref="table" v-loading="loading" :data="dataList" :height="tableHeight" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" :index="table_index" width="55" align="center" prop="pggId"/>
      <el-table-column label="组盘名称" prop="pggName" align="center" :show-overflow-tooltip="true"  />
      <el-table-column label="信息包大小" prop="size" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.size + 'MB' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数量(个)" prop="quantity" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.quantity == '0' ? '' : scope.row.quantity }}</span>
        </template>
      </el-table-column>
      <el-table-column label="组盘状态" prop="state" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.state == '1' ? '已组盘' : '待组盘' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="400">
        <template slot-scope="scope">
          <el-button type="primary"  plain size="small" @click="zpRule(scope.row)">组盘规则</el-button>
          <el-button type="success"  plain size="small" @click="handleDetail(scope.row)" :disabled="scope.row.state == 0">查看详情</el-button>
          <el-button type="danger"  plain size="small" @click="cancelZp(scope.row)" :disabled="scope.row.state == 0">取消组盘</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加/修改组盘 -->
    <el-dialog :title="title" :visible.sync="open" width="45%" append-to-body>
      <el-form ref="zpForm" :model="zpForm" :rules="zpRules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="载体类型" prop="ztlxId">
              <el-select v-model="zpForm.ztlxId" placeholder="请选择载体类型" clearable style="width: 100%;" :disabled="isDisabled">
                <el-option v-for="item in carrierOption" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组盘名称" prop="pggName">
              <el-input v-model="zpForm.pggName" placeholder="组盘名称" style="width: 100%;" :disabled="isDisabled" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="信息包大小" prop="size">
              <el-input-number v-model="zpForm.size" controls-position="right" placeholder="请输入" :min="1" style="width: 88%;" :disabled="isDisabled"></el-input-number> (MB)
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="zpForm.remark" type="textarea" placeholder="请输入内容" ref="remarkRef" :disabled="isDisabled"/>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="isDisabled">保 存</el-button>
        <el-button  @click="zpCancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 组盘规则 -->
    <ZpRule :open="zpRuleOpen" @closeDelDialog="closeDelDialog" :dataInfo="dataInfo" ref="zpRuleRef"></ZpRule>

    <!-- 查看详情 -->
    <el-dialog title="详情" :visible.sync="zpOpen" width="70%" append-to-body :before-close="closeDelDialog">
      <div style="width:90%;margin:20px auto;height:650px;overflow-y: auto">
        <ZpPlan ref="zpfaRef" :isType="3"></ZpPlan>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import estimateTableHeight from "@/mixins/estimateTableHeight";
import {
  getTableList,
  getCarrier,
  addZp,
  updateZp,
  getZpDetail,
  deleteZp, getDetail, cancelZp
} from "@/api/archive/filesManage/diskManageMenu";
import ZpRule from "@/views/archive/filesManage/diskManageMenu/zpRule";
import ZpPlan from "@/views/archive/filesManage/diskManageMenu/zpPlan";

export default {
  name: "index",
  components: {ZpPlan, ZpRule},
  mixins: [estimateTableHeight], // 混入方法
  data() {
    return {
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      dataList: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },

      title: '',
      open: false,
      zpForm: {
        ztlxId: undefined,
        pggName: undefined,
        size: undefined,
        remark: undefined
      },
      zpRules: {
        ztlxId: [
          { required: true, message: "载体类型不能为空" }
        ],
        pggName: [
          { required: true, message: "组盘名称不能为空" }
        ],
        size: [
          { required: true, message: "信息包大小不能为空" }
        ],
      },
      // 载体类型
      carrierOption: [],
      zpRuleOpen: false,
      dataInfo: {},
      isDisabled: false,
      zpOpen: false
    }
  },
  created() {
    this.getList()
    this.getCarrierOption()
  },
  methods: {
    // 获取载体类型
    getCarrierOption() {
      getCarrier().then(response=>{
        this.carrierOption = response.data.map(item=>{
          return {
            value: item.ztlxId,
            label: item.ztlxName
          }
        })
      })
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pggId)
      this.state = selection.map(item => item.state)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    table_index(index){
      return (this.queryParams.pageNum-1) * this.queryParams.pageSize + index + 1;
    },
    getList() {
      this.loading = true
      getTableList(this.queryParams).then(response=>{
        this.dataList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    reset() {
      this.zpForm = {
        ztlxId: undefined,
        pggName: undefined,
        size: undefined,
        remark: undefined
      }
      this.$nextTick(() => {
        this.$refs.remarkRef.$refs.textarea.style.height = 'auto'; // 重置高度
      });
    },
    // 新增
    handleAdd() {
      this.reset()
      this.isDisabled = false
      this.resetForm('zpForm')
      this.title = '新增组盘'
      this.open = true
    },
    // 修改
    handleUpdate(row) {
      if (this.single == false) {
        this.reset()
        this.resetForm('zpForm')
        const id = row.pggId || this.ids
        this.isDisabled = false
        if (this.state.includes(1)) {
          this.isDisabled = true
        }
        getZpDetail(id).then(response=>{
          this.zpForm = response.data
          this.title = '修改组盘'
          this.open = true
        })
      } else {
        this.$message({ message: '请选择一条数据进行修改', type: 'warning' })
      }
    },
    submitForm() {
      this.$refs["zpForm"].validate(valid => {
        if (valid) {
          if (this.zpForm.pggId != undefined) {
            updateZp(this.zpForm).then(response=>{
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            })
          } else {
            addZp(this.zpForm).then(response=>{
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            })
          }
        }
      })
    },
    zpCancel() {
      this.open = false
    },
    // 删除
    handleDelete(row) {
      const ids = row.pggId || this.ids;
      if (ids.length !== 0) {
        if (this.state.includes(1)) {
          this.$modal.msgWarning('所选数据包含已组盘数据,不能进行删除操作')
        } else {
          this.$modal.confirm('是否确认删除数据？').then(function () {
            return deleteZp(ids)
          }).then(response => {
            this.getList();
            this.$modal.msgSuccess(response.msg);
          }).catch(() => {});
        }
      } else {
        this.$modal.msgError("请选择要删除的数据");
      }
    },
    // 组盘规则
    zpRule(row) {
      this.zpRuleOpen = true
      this.$nextTick(() => {   //此函数执行时所有的DOM挂载和渲染都已完成
        this.$refs.zpRuleRef.activeName = 1
        this.$refs.zpRuleRef.init(row.pggId, row.state);     //获取子组件中init方法并将父组件id传递给子组件
      });
    },
    closeDelDialog() {
      this.zpRuleOpen = false
      this.zpOpen = false
      this.getList()
    },
    // 查看详情
    handleDetail(row) {
      getDetail(row.pggId).then(res=>{
        this.zpOpen = true
        this.$nextTick(() => {
          this.$refs.zpfaRef.isShow = false
          this.$refs.zpfaRef.init(res.data);
        })
      })
    },
    // 取消组盘
    cancelZp(row) {
      const id = row.pggId
      this.$modal.confirm('是否确认取消组盘？').then(function () {
        return cancelZp(id)
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess('取消成功');
      }).catch(() => {});
    }
  }
}
</script>

<style scoped>

</style>
